/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.br.comps;

import coalescent.br.AMCElement;
import coalescent.br.BRWComp;
import coalescent.br.BRWCompInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * An adapter for the listener interface of the post-order recursion implementation.<p>
 * It provides default implementations for all the methods and the subclasses can override only the
 * methods they are interested in. Subclasses should look at the javadoc of the overriding method to
 * see if the {@code super} method needs to be called. Failing to do so may throw
 * {@code IllegalStateException}.
 *
 * @author Susanta Tewari
 * @version 1.0 Dec 4, 2010
 *
 * @param <E>
 * @param <S>
 */
public class BRWCompAdapter<E extends Enum<E>, S extends AMCElement<E, S>>
        implements BRWComp<E, S> {

    /** Field description */
    private static final List<Class<? extends BRWComp>> listeners = new ArrayList<>();

    static {

        listeners.add(BRWACCounter.class);
        listeners.add(BRWACBuilder.class);
        listeners.add(BRWProbComputer.class);
        listeners.add(BRWGenealogyCounter.class);
        listeners.add(BRWGenealogyBuilder.class);
    }

    /** Field description */
    private boolean recursionFinsihed = false;

    /**
     * A new instance from the specified class name of a computation.
     *
     * @param computationClassName fully qualified class name that implements
     * {@code BRWComp}
     * @return a new instance of the specified computation class or {@code null} if an instance
     *         could not be created
     */
    public static BRWComp getInstance(String computationClassName) {

        for (Class<? extends BRWComp> cls : listeners) {

            if (cls.getName().equals(computationClassName)) {

                try {
                    return cls.newInstance();
                } catch (Exception ex) {
                    Logger.getLogger(BRWCompAdapter.class.getName()).log(Level.WARNING, null, ex);
                }
            }
        }

        return null;
    }

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onBoundaryCondn(BRWCompInfo<E, S> event) {}

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onStartingIteratingEvents(BRWCompInfo<E, S> event) {}

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onIteratingSingleEventTypes(BRWCompInfo<E, S> event) {}

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onIteratingAncestralConfigs(BRWCompInfo<E, S> event) {}

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onFinishedIteratingSingleEventTypes(BRWCompInfo<E, S> event) {}

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void onFinishedIteratingEvents(BRWCompInfo<E, S> event) {}

    /**
     * {@inheritDoc}<p>
     * Must be called by the overriding method as the first statement.
     */
    @Override
    public void onFinishedIteration(BRWCompInfo<E, S> event) {
        recursionFinsihed = true;
    }

    /**
     * An empty string. The subclasses should override this method to provide a brief description
     * on the results.<p>
     * Must be called by the overriding method as the first statement.
     *
     * @return empty string
     */
    @Override
    public String toString() {

        if (!recursionFinsihed) {

            throw new IllegalStateException(
                "this method should be invoked only after the recursion is over "
                + "succeessfully.");
        }

        return "";
    }

    /**
     * Should be overridden to provide relevant timer tasks
     *
     * @return
     */
    @Override
    public TimerTask createTimerTask() {
        return null;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public String getUpdateChunk() {
        return null;
    }
}
